package com.yyk.yiliao.ui.trade.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yyk.yiliao.R;
import com.yyk.yiliao.util.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.android.agoo.common.d.TAG;

/**
 * 280357392/ZFPlay: 支付密码输入框  https://github.com/280357392/ZFPlay
 */

public class PhoneSmsView extends RelativeLayout {

    Context context;
    private String strPassword; // 输入的密码
    private TextView[] tvList; // 就6个输入框不会变了，用数组内存申请固定空间，比List省空间
    private GridView gridView; // 用GrideView布局键盘，其实并不是真正的键盘，只是模拟键盘的功能
    private ArrayList<Map<String, String>> valueList; // 要用Adapter中适配，用数组不能往adapter中填充
    private ImageView tvCancel;//取消按钮
    private int currentIndex = -1; // 用于记录当前输入密码格位置


    private EditText editText; //短信验证码
    private Button btNext; //确定按钮
    private TextView tvHqsms;//获取验证码
    private TextView tvBankel;//银行预留手机号码

    public PhoneSmsView(Context context) {
        this(context, null);
    }

    public PhoneSmsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        //view布局
        View view = View.inflate(context, R.layout.layout_popup_phonesms, null);
        valueList = new ArrayList<>();
        //初始化控件
        tvCancel = (ImageView) view.findViewById(R.id.tvCancel);


        editText = (EditText) view.findViewById(R.id.et_sms);
        btNext = view.findViewById(R.id.bt_next);
        tvHqsms = view.findViewById(R.id.tv_hqsms);

        tvBankel = view.findViewById(R.id.tv_bankel);


        //初始化键盘
        gridView = (GridView) view.findViewById(R.id.gv_keybord);
        //设置键盘显示按钮到集合
        setView();

        // 必须要，不然不显示控件
        addView(view);
    }

    private List<String> list = new ArrayList<>();

    //设置按钮显示内容
    private void setView() {

        // 初始化按钮上应该显示的数字
        for (int i = 1; i < 13; i++) {
            Map<String, String> map = new HashMap<>();
            if (i < 10) {
                map.put("name", String.valueOf(i));
            } else if (i == 10) {
                map.put("name", "");
            } else if (i == 12) {
                map.put("name", "<<-");
            } else if (i == 11) {
                map.put("name", String.valueOf(0));
            }
            valueList.add(map);
        }

        //为键盘gridview设置适配器
        gridView.setAdapter(adapter);

        //为键盘按键添加点击事件
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                switch (position) {
                    case 9:
                        break;
                    case 11:
                        int currentIndex = editText.getSelectionStart();
                        if (currentIndex > 0) {
                            editText.getText().delete(currentIndex - 1, currentIndex);
//                            if (videoIdEt.getText().length() == 0) {
//                                //设置 ClickableEditText中只显示左边图标，删除图标不可见
//                                editText.setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null,null);
//                                findViewById(R.id.ai_long_tv_confirm).setBackgroundResource(R.drawable.bg_determine_disable);
//                            }
                        }
                        break;

                    default:
                        //获取当前光标位置
                        int index = editText.getSelectionStart();
                        if (index != editText.getText().length()) {
                            //在光标处插入数字
                            String inputEditText = valueList.get(position).get("name");
                            Log.d(TAG, "inputEditText:" + inputEditText);
                            editText.getText().insert(index, inputEditText);
                        } else {
                            editText.setText(editText.getText().toString().trim() + valueList.get(position).get("name"));
                            editText.setSelection(editText.getText().length());
                        }
//                        验证码右边 减号
//                        if (editText.getText().length() > 0) {
//                            findViewById(R.id.ai_long_tv_confirm).setBackgroundResource(R.drawable.bg_determine_text);
//                            Drawable drawable = getResources().getDrawable(R.drawable.ic_delete1);
                        //设置 ClickableEditText中只显示左边图标，删除图标可见
//                            editText.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable,null);
//                }
                        break;
                }


//                // 点击0~9按钮
//                if (position < 11 && position != 9) {
//                    // 判断输入位置————要小心数组越界
//                    if (currentIndex >= -1 && currentIndex < 5) {
////                        tvList[++currentIndex].setText(valueList.get(position).get("name"));
//                        list.add(valueList.get(position).get("name"));
//                        for (String string:list ){
//                            editText.setText(string);
//                        }
//
//                    }
//                } else {
//                    // 点击退格键
//                    if (position == 11) {
//                        // 判断是否删除完毕————要小心数组越界
//                        if (currentIndex - 1 >= -1) {
////                            tvList[currentIndex--].setText("");
//                            String string = editText.getText().toString().trim();
//                            String substring = string.substring(0, string.length() - 1);
//                            editText.setText(substring);
//                        }
//                    }
//                }
            }
        });
    }

    // 设置监听方法，在第6位输入完成后触发
    public void setOnFinishInput(final OnPasswordInputFinish pass) {


        btNext.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                strPassword = editText.getText().toString().trim();

                // 接口中要实现的方法，完成密码输入完成后的响应逻辑
                pass.inputFinish();
            }
        });
        tvCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击取消调用接口
                pass.outfo();
            }
        });
        tvHqsms.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击取消调用接口
                pass.forgetPwd();
            }
        });
    }

    //获取输入的密码
    public String getStrPassword() {
        return strPassword;
    }

    // GrideView的适配器
    BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return valueList.size();
        }

        @Override
        public Object getItem(int position) {
            return valueList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder viewHolder;
            if (convertView == null) {
                //装载数字键盘布局
                convertView = View.inflate(context, R.layout.adapter_item_tade, null);
                int w = (ScreenUtil.getScreenWidth(context) - ScreenUtil.dip2px(context, 1)) / 3;
                int h = (ScreenUtil.getScreenWidth(context) - ScreenUtil.dip2px(context, 1)) / 18 * 3;
                ////设置convertView的LayoutParams
                convertView.setLayoutParams(new AbsListView.LayoutParams(w, h));
                viewHolder = new ViewHolder();
                //初始化键盘按钮
                viewHolder.btnKey = convertView.findViewById(R.id.btn_keys);
                viewHolder.ivKey = convertView.findViewById(R.id.iv_del);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.btnKey.setText(valueList.get(position).get("name"));

            //设置按钮显示数字
            if (position == 9) {
                //设置左边空白按钮背景
//                viewHolder.btnKey.setBackgroundResource(R.drawable.selector_key_del);
                //设置按钮不可点击
                viewHolder.btnKey.setEnabled(false);
                viewHolder.btnKey.setVisibility(VISIBLE);
                viewHolder.ivKey.setVisibility(GONE);
            } else if (position == 11) {
                //设置右边删除按钮背景
//                viewHolder.btnKey.setBackgroundResource(R.drawable.selector_key_del);
                viewHolder.btnKey.setVisibility(GONE);
//                viewHolder.btnKey.setCompoundDrawables(null,null,R.drawable.selector_key_del);
                viewHolder.ivKey.setVisibility(VISIBLE);
            } else {
                viewHolder.btnKey.setText(valueList.get(position).get("name"));
                viewHolder.btnKey.setVisibility(VISIBLE);
                viewHolder.ivKey.setVisibility(GONE);

            }

            return convertView;
        }

    };

    public final class ViewHolder {
        public TextView btnKey;
        public ImageView ivKey;
    }
}